package com.woniu.deal.order.consumer;

import com.rabbitmq.client.Channel;
import com.woniu.common.entity.DealOrder;
import com.woniu.deal.order.feignclient.ElderlyWaresFeign;
import com.woniu.deal.order.mapper.DealOrderMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class InsertOrderConsumer {
    @Autowired
    DealOrderMapper dealOrderMapper;
    @Autowired
    ElderlyWaresFeign elderlyWaresFeign;

    //这里应该用seata的事务控制管理起来
    @RabbitListener(queues = "queueinsertorder")
    public synchronized void doqueueinsertorder(DealOrder dealOrder, Channel channel, Message message){
        System.out.println(dealOrder);
        //在这里调用添加订单和减少库存的方法
        dealOrderMapper.insert(dealOrder);
        elderlyWaresFeign.updateWaresSkuStock(dealOrder.getSkuId(),1);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException("订单添加异常");
        }
    }

}
